package com.topscore.uke.ui.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.topscore.uke.R;
import com.topscore.uke.entities.PendingAuditEntity;
import com.topscore.uke.ui.BaseSupportFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class PendingAuditFragment extends BaseSupportFragment {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.select_all_check_box)
    CheckBox selectAllCheckBox;

    private View rootView;

    private RecyclerAdapter mAdapter;

    private List<PendingAuditEntity> mSelectedItemList;

    public PendingAuditFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_pending_audit, container, false);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mAdapter = new RecyclerAdapter();
        recyclerView.setAdapter(mAdapter);
        selectAllCheckBox.setOnClickListener(v -> {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                if (mAdapter.getData().get(i).isSelected() != selectAllCheckBox.isChecked()){
                    mAdapter.getData().get(i).setSelected(selectAllCheckBox.isChecked());
                }
            }
            mAdapter.notifyDataSetChanged();
        });
        initData();
    }

    private void initData() {
        List<PendingAuditEntity> entityList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            entityList.add(new PendingAuditEntity("王建国", "男"));
        }
        mAdapter.setNewData(entityList);

    }

    private void setUpSelectAll(List<PendingAuditEntity> data) {
        mSelectedItemList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isSelected()) {
                mSelectedItemList.add(data.get(i));
            }
        }

        selectAllCheckBox.setChecked(mSelectedItemList.size() == data.size());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private class RecyclerAdapter extends BaseQuickAdapter<PendingAuditEntity, BaseViewHolder> {

        public RecyclerAdapter() {
            super(R.layout.pending_audit_item, new ArrayList<>());
        }

        @Override
        protected void convert(BaseViewHolder holder, PendingAuditEntity item) {
            holder.setText(R.id.name_tv, item.getName())
                    .setText(R.id.gender_tv, item.getGender());
            if (item.isSelected()) {
                holder.setImageResource(R.id.select_iv, R.mipmap.check_box_round_selected);
            } else {
                holder.setImageResource(R.id.select_iv, R.mipmap.check_box_round_unselect);
            }

            holder.getView(R.id.select_iv).setOnClickListener(v -> {
                if (item.isSelected()) {
                    item.setSelected(false);
                } else {
                    item.setSelected(true);
                }
                notifyItemChanged(holder.getPosition(), "select");
                setUpSelectAll(getData());
            });


        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position, List<Object> payloads) {
            if (payloads.isEmpty()) {
                onBindViewHolder(holder, position);
            } else {
                PendingAuditEntity item = getData().get(position);
                if (item.isSelected()) {
                    holder.setImageResource(R.id.select_iv, R.mipmap.check_box_round_selected);
                } else {
                    holder.setImageResource(R.id.select_iv, R.mipmap.check_box_round_unselect);
                }

            }
        }

    }
}
